#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include "Log.hpp"
using namespace LogModule;

class Session
{
public:
    Session(const std::string &username, const std::string &status)
         : _usename(username), _status(status)
    {
        _create_time = time(nullptr);
    }

    std::string UserName() const { return _usename; }

    ~Session()
    {}
private:
    std::string _usename;
    std::string _status;
    uint64_t _create_time;
};

using session_ptr = std::shared_ptr<Session>;

class SessionManager
{
public:
    SessionManager()
    {
        srand(time(nullptr) ^ getpid());
    }

    std::string AddSession(session_ptr s)
    {
        uint32_t randomid = rand() + time(nullptr); // 随机数+时间戳，还有其他的库如boost uuid库，可以形成sessionid
        std::string sessionid = std::to_string(randomid);
        _sessions.insert(std::make_pair(sessionid, s));
        std::cout << "插入中########################" << std::endl;
        for(auto &s: _sessions)
        {
            std::cout << "Session: " << s.first << "->" << s.second->UserName() << std::endl;
        }
        std::cout << "插入中########################" << std::endl;
        return sessionid;
    }

    session_ptr GetSession(const std::string &sessionid)
    {
        if(_sessions.find(sessionid) == _sessions.end())
            return nullptr;
        return _sessions[sessionid];
    }

    std::string DebugPrint()
    {
        std::string ret;
        for(auto &s: _sessions)
        {
            std::string tmp = "Session: " + s.first + "->" + s.second->UserName() + "\n";
            ret += tmp;
        }
        return ret;
    }

    void DeleteSession(uint64_t session_id)
    {}

private:
    std::unordered_map<std::string, session_ptr> _sessions; 
};